package com.zm.zyz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zm.zyz.entity.ZyzJoins;
import com.zm.zyz.mapper.ZyzJoinsMapper;
import com.zm.zyz.util.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.function.Consumer;

    public class ZyzBiz {
        public void join(String tid,Integer uid) throws BizException {
            Object ret =  Utils.exec(sqlSession->{
                ZyzJoinsMapper joinsMapper = sqlSession.getMapper(ZyzJoinsMapper.class);
                LambdaQueryWrapper<ZyzJoins> lqw = new LambdaQueryWrapper<>();
                lqw.eq(ZyzJoins::getTid,tid);
                lqw.eq(ZyzJoins::getUid,uid);
                Integer count = joinsMapper.selectCount(lqw);
                if(count>0){
                    return "该用户已经加入当前活动了";
                }
                ZyzJoins joins = new ZyzJoins();
                joins.setTid(Integer.valueOf(tid));
                joins.setUid(uid);
                joinsMapper.insert(joins);
                return null;
            });
            if (ret!=null){
                throw new BizException(ret.toString());
            }
        }
}
